package app;

import org.rygh.core.xml.XmlLeaf;

import wsi.function.Function;
import wsi.function.FunctionController;
import wsi.function.RuntimeInfo;
import wsi.script.AppConfig;
import wsi.script.AppTag;
import wsi.task.Engine;

public class runOnce extends App {

	public static void main(String... args) {
		init();
		run();
		bye();
	}

	public static String run() {
		AppConfig.init();

		AppConfig ac = AppConfig.shared();
		Engine engine = new Engine();
		Function function;
		RuntimeInfo iconfig;
		String service, func_name;
		FunctionController fctrl = FunctionController.shared();

		for (XmlLeaf serviceNode : ac.getOnServices()) {
			service = serviceNode.getValue().getAttribute(AppTag.name);

			for (XmlLeaf functionNode : ac.getOnFunctions(serviceNode)) {
				func_name = functionNode.getValue().getAttribute(AppTag.name);

				function = fctrl.getFunction(service, func_name);
				assert function != null : String.format("%s/%s", service,
						func_name);

				iconfig = ac.getConfig(function);

				if (iconfig == null)
					continue;

				function.setRuntimeInfo(iconfig);
				engine.process(function);
			}
		}

		return engine.save();
	}

}
